// index.js
// 获取应用实例
const app = getApp()

Page({
    data: {
        // 输入框的值
        value: '',
        // todo列表
        list: [],
        sum: 0,
        sum1: 0
    },
    // 输入框事件
    writeTouch(e) {
        // console.log(e.detail.value);
        this.setData({
            value: e.detail.value
        })
    },
    // 回车添加todo
    handleAdd() {
        if (this.data.value == '') return
        this.data.list.push({
            checked: false,
            title: this.data.value,
        })
        this.setData({
            list: this.data.list,
            value: ''
        })
        wx.setStorageSync('todo', this.data.list)
        this.getSum()
    },
    // 修改状态
    changeStatus(e) {
        // console.log(e.currentTarget.dataset.index);
        let index = e.currentTarget.dataset.index
        this.data.list[index].checked = !this.data.list[index].checked
        this.setData({
            list: this.data.list
        })
        wx.setStorageSync('todo', this.data.list)
        this.getSum()
    },
    // 删除todo
    delTodo(e) {
        // console.log(e.currentTarget.dataset.index);
        this.data.list.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            list: this.data.list
        })
        wx.setStorageSync('todo', this.data.list)
        this.getSum()
    },
    getSum() {
        let n0 = 0
        let n1 = 0
        this.data.list.forEach(item => {
            if (item.checked == false) {
                n0 += 1
            } else {
                n1 += 1
            }
        })
        this.setData({
            sum: n0,
            sum1: n1
        })
    },
    onLoad() {
        this.data.list = wx.getStorageSync('todo') || []
        this.setData({
            list: this.data.list,
        })
        this.getSum()
    },


})